//
//  VKStudyCollectController.m
//  V-Learn
//
//  Created by liubo on 2019/2/12.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKStudyCollectController.h"
#import "VKStudyCollectModel.h"
#import "VKStudyCollectCell.h"
@interface VKStudyCollectController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation VKStudyCollectController
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 500; /// 预估行高
        [self.view addSubview:_tableView];
        //        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"收藏";
    [self setupData];
    [self setupRightItem];
}
// 右边Item
- (void)setupRightItem{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"管理" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor cz_colorWithRed:115 green:115 blue:115] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button] ;
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@30);
        make.width.equalTo(@70);
    }];
    [button addTarget:self action:@selector(buttonItemClick) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonItemClick{
//    [self.navigationController pushViewController:[[VKStudyCardRuleController alloc]init] animated:YES];
}

- (void)setupData{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";
    [hud show:YES];
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKMeQueryProtocols withPostParams:@{}.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        if ([result[@"code"]integerValue] == 200) {
            
            for (NSDictionary *dic in result[@"data"]) {
                VKStudyCollectModel *model = [VKStudyCollectModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
        [self.tableView reloadData];
    }];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKStudyCollectCell *cell = [VKStudyCollectCell cellWithTableView:tableView];
    VKStudyCollectModel *model = self.dataArray[indexPath.row];
    model.row = indexPath.row;
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
